﻿using System;
using System.Collections.Generic;
using System.Linq;
using JustinAngel.MetroBlog.Services.Data;
using JustinAngel.MetroBlog.Services.Repository;
using Microsoft.Practices.Unity;
using JustinAngel.MetroBlog.Common.Helpers;

namespace JustinAngel.MetroBlog.Services.CategoryService
{
    public class CategoryService : ICategoryService
    {
        private Comparison<BlogPost> blogPostDateComprar = ((b1, b2) =>
                                                               {
                                                                   if (b1.DateCreated < b2.DateCreated)
                                                                       return -1;
                                                                   if (b1.DateCreated > b1.DateCreated)
                                                                       return 1;
                                                                   else
                                                                       return 0;
                                                               });

        [Dependency]
        public IRepository<Category> CategoryRepository { get; set; }

        public Category GetCatrgoryOrCreateNewOne(string curCategoryString)
        {
           Category curCategory =
                CategoryRepository.Get().FirstOrDefault(c => c.Name == curCategoryString);

            if (curCategory == null)
            {
                curCategory = new Category() { Name = curCategoryString };
                CategoryRepository.Insert(curCategory);
            }
            return curCategory;
        }

        public IList<BlogPost> GetTopBlogPostsInCategoryForUser(Category curCategory, int NumberOfBlogPosts, string Username)
        {
            List<BlogPost> ReturnValues = new List<BlogPost>();

            GetTopBlogPostsInCategory(curCategory, ReturnValues, Username);
            
            ReturnValues.Sort(blogPostDateComprar);

            return ReturnValues.Take(NumberOfBlogPosts).ToList();
        }

        public bool Exists(string link)
        {
            return CategoryRepository.Get().Any(c => c.Name == link
                                                  || c.HomePageName == link);
        }

        public Category GetByLink(string link)
        {
            return CategoryRepository.Get()
                                     .With(c => c.PostCategories)
                                     .With(c => c.PostCategories.Ignore().BlogPost)
                                     .FirstOrDefault(c => c.Name == link
                                                       || c.HomePageName == link);
        }

        private void GetTopBlogPostsInCategory(Category curCategory, List<BlogPost> ReturnValues, string Username)
        {
            if (curCategory == null)
                return;

            curCategory.Posts.Load();
            curCategory.ChildCategories.Load();

            ReturnValues.AddRange(curCategory.Posts.Where(b =>
                                                              {
                                                                  b.UserReference.Load();
                                                                  return b.User.Username == Username;
                                                              }).Take(5).ToList());

            foreach (Category childCategory in curCategory.ChildCategories)
            {
                if (curCategory != childCategory)
                    GetTopBlogPostsInCategory(childCategory, ReturnValues, Username);
            }
        }
    }


}
